package cn.it.shop.action;

import java.lang.reflect.ParameterizedType;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Controller;

import cn.it.shop.lucene.LuceneService;
import cn.it.shop.service.AccountService;
import cn.it.shop.service.CategoryService;
import cn.it.shop.service.EmailUtil;
import cn.it.shop.service.FileUploadUtil;
import cn.it.shop.service.ForderService;
import cn.it.shop.service.GoodsService;
import cn.it.shop.service.PayService;
import cn.it.shop.service.PrivilegeService;
import cn.it.shop.service.RoleService;
import cn.it.shop.service.SendMessageUtil;
import cn.it.shop.service.SorderService;
import cn.it.shop.service.UserService;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/*
 * 此Action与BaseService一样,不需要自己创建, 是子类间接实例化
 * */
@Controller
@Scope("prototype")
public class BaseAction<T> extends ActionSupport implements RequestAware,
		SessionAware, ApplicationAware, ModelDriven<T> {

	protected T model;

	@Resource(name="categoryService")  // 查找: @Service(value="categoryService")
	protected CategoryService categoryService = null;
	/*
	 * 默认查找的就是与当前属性名称同名bean, 在没有查找名称的前提下, 如果通过byName没有找到自动会通过byType
	 * 如果配置了 name="accountService" 那么只能通过byName找,找不到则抛出异常
	 * */
	@Resource/*(name="accountService")*/
	protected AccountService accountService=null;
	@Resource
	protected LuceneService luceneService=null;
	@Resource
	protected GoodsService goodsService=null;
	@Resource
	protected FileUploadUtil fileUploadUtil=null;
	@Resource
	protected SorderService sorderService=null;
	@Resource
	protected ForderService forderService=null;
	@Resource
	protected UserService userService=null;
	@Resource
	protected PayService payService=null;
	@Resource
	protected EmailUtil emailUtil=null;
	@Resource
	protected SendMessageUtil sendMessageUtil=null;
	@Resource
	protected RoleService roleService=null;
	@Resource
	protected PrivilegeService privilegeService=null;
	@Resource(name="privilegeService")
	protected FilterInvocationSecurityMetadataSource securityMetadataSource=null;
	
	protected Map<String, Object> request;
	protected Map<String, Object> session;
	protected Map<String, Object> application;
	
	@Override
	@SuppressWarnings("unchecked")
	public T getModel() {
		// 通过子类来确定T的类型
		ParameterizedType type = (ParameterizedType) this.getClass()
				.getGenericSuperclass();
		Class clazz = (Class) type.getActualTypeArguments()[0];
		try {
			model = (T) clazz.newInstance();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return model;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	@Override
	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session = session;
	}
}
